//editRoleCtrl

app.controller('rolePermissionCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	$scope.iNowData = angular.copy($scope.ngDialogData, {});
	var token = $window.localStorage.getItem("token");
    $scope.init = function(){
		
		//用于添加当前高亮
		$scope.moduleIndex = 0, 
		$scope.menuIndex = 0;
		
		//存储属树形结构
		$scope.myDatas = [];
		
		//请求select角色列表
    	rest.decoJava({
    		type: 'post',
	        url: '/tenancy-sys/admin/role/all/list',  ///settlement-center-service/admin/role/list.do
	        data: '',
	        beforeSend: function(request){
				request.setRequestHeader("token", token);
			},
	        success: function(data){
	        	if(data.code == "0000"){
		          	$scope.$apply(function(){
		          	 	$scope.roles = data.result;
		          	 	angular.forEach($scope.roles,function(data){
		          	 		console.log(data.id,$scope.iNowData.id)
		          	 		if(data.id == $scope.iNowData.id){
		          	 			
		          	 			$scope.selectedRole = data;
		          	 			$scope.changeRole($scope.selectedRole);  //主动执行第一次
		          	 		}
		          	 	})
		          	})
		        } else{
		        	layer.msg(data.message,{
	    				offset:'t'
	    			});
		        }
	        }
	    })
    	
	}
	$scope.init();
	
	//改变授权角色的时候
	$scope.changeRole = function(obj){
		$scope.accessStrId = obj.id;
		//请求菜单及方法
		rest.decoJava({
			type: 'post',
	        url: '/tenancy-sys/admin/role/accessTree/'+obj.id,
	        data: '',
	        beforeSend: function(request){
				request.setRequestHeader("token", token);
			},
	        success: function(data){
	        	if(data.code == "0000"){
	        		$scope.moduleDatas = data.result;
	        		$scope.$apply();
	        	} else{
	        		layer.msg(data.message,{
	    				offset:'t'
	    			});
	        	}
	        }
	    });
	}
	
	//按钮是否选中 ng-class
	$scope.anniuClass = function(isSelected){
		var className = "";
		if(isSelected == 1){
			return className += "checkd";
		}
		return className;
	};
	
	//模块按钮方法
	$scope.checkBoxMk = function(m){
		var fnId = "";
		console.log(m)
		if(m.menuVo.isSelected != 1){  //模块无权限 即添加权限
			fnId = m.menuVo.id;  //一级菜单ID
			//选中操作
			rest.decoJava({
				type: 'post',
				url: '/tenancy-sys/admin/role/access/add/'+$scope.accessStrId,
				data: {accessStr: fnId},
				beforeSend: function(request){
					request.setRequestHeader("token", token);
				},
				success: function(res){
					if(res.code == "0000"){
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 6
					    });
						m.menuVo.isSelected = 1;  //前端伪改变状态
						$scope.$apply();
					} else{
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 5
					    });
					}
				}
			})
			
		} else{ //模块有权限 即移除权限
			
			fnId = m.menuVo.id; //数据：菜单ID
			//遍历m.childList菜单下的二级菜单是否有未移除状态
			var onOff = true; //开关 控制是否满足删除条件
			angular.forEach(m.childList,function(data){
				if(data.menuVo.isSelected == 1 && onOff == true){
					onOff = false;
				}
			})
			if(!onOff){
				layer.msg("该菜单下有尚未移除的权限,当前操作不可用", {
			    	offset:'t',
			      	icon: 7
			    });
			} else{
				
				//移除操作
				rest.decoJava({
					type: 'post',
					url: '/tenancy-sys/admin/role/access/remove/'+$scope.accessStrId,
					data: {accessStr: fnId},
					beforeSend: function(request){
						request.setRequestHeader("token", token);
					},
					success: function(res){
						if(res.code == "0000"){
							layer.msg(res.message, {
						    	offset:'t',
						      	icon: 6
						   });
							m.menuVo.isSelected = null;  //前端伪改变状态
							$scope.$apply();
						} else{
							layer.msg(res.message, {
						    	offset:'t',
						      	icon: 5
						    });
						}
					}
				})
				
			}
			
		}
	}
	
	//菜单按钮方法
	$scope.checkBoxCd = function(m,c){
		var fnId = "";
		if(c.isSelected != 1){  //菜单无权限
			
			if(m.isSelected != 1){  //模块无权限
				fnId = m.id +','+c.id;
			} else{ //模块已有权限
				fnId = c.id;
			}
			
			//选中操作
			rest.decoJava({
				type: 'post',
				url: '/tenancy-sys/admin/role/access/add/'+$scope.accessStrId,
				data: {accessStr: fnId},
				beforeSend: function(request){
					request.setRequestHeader("token", token);
				},
				success: function(res){
					if(res.code == "0000"){
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 6
					    });
					    m.isSelected = 1;
						c.isSelected = 1;  //前端伪改变状态
						$scope.$apply();
					} else{
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 5
					    });
					}
				}
			})
		} else{  //移除
			
			fnId = c.id; //数据：菜单ID
			//遍历c.menuMethods菜单下的方法是否有未移除状态
			var onOff = true; //开关 控制是否满足删除条件
			angular.forEach(c.menuMethods,function(data){
				if(data.isSelected == 1 && onOff == true){
					onOff = false;
				}
			})
			if(!onOff){
				layer.msg("该菜单下有尚未移除的权限,当前操作不可用", {
			    	offset:'t',
			      	icon: 7
			    });
			} else{
				
				//移除操作
				rest.decoJava({
					type: 'post',
					url: '/tenancy-sys/admin/role/access/remove/'+$scope.accessStrId,
					data: {accessStr: fnId},
					beforeSend: function(request){
						request.setRequestHeader("token", token);
					},
					success: function(res){
						if(res.code == "0000"){
							layer.msg(res.message, {
						    	offset:'t',
						      	icon: 6
						   });
							c.isSelected = null;  //前端伪改变状态
							$scope.$apply();
						} else{
							layer.msg(res.message, {
						    	offset:'t',
						      	icon: 5
						    });
						}
					}
				})
				
			}
		}
	}
	
	//方法按钮的方法
	$scope.checkBoxFn = function(m,c,f){
		var fnId = "";
		
		if(f.isSelected != 1){  //方法无权限 添加
			if(m.isSelected != 1){  //模块无权限
				fnId = m.id +',';
			}
			if(c.isSelected != 1){  //菜单无权限
				fnId += c.id + ',';
			}
			fnId += c.id+'_'+f.id;
			
			//选中操作
			rest.decoJava({
				type: 'post',
				url: '/tenancy-sys/admin/role/access/add/'+$scope.accessStrId,
				data: {accessStr: fnId},
				beforeSend: function(request){
					request.setRequestHeader("token", token);
				},
				success: function(res){
					if(res.code == "0000"){
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 6
					    });
					    m.isSelected = 1;
					    c.isSelected = 1;
						f.isSelected = 1;  //前端伪改变状态
						$scope.$apply();
					} else{
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 5
					    });
					}
				}
			})
		} else{  //移除
			
			fnId = c.id+'_'+f.id;  //数据类型：菜单ID,菜单ID_方法ID
			
			//选中操作
			rest.decoJava({
				type: 'post',
				url: '/tenancy-sys/admin/role/access/remove/'+$scope.accessStrId,
				data: {accessStr: fnId},
				beforeSend: function(request){
					request.setRequestHeader("token", token);
				},
				success: function(res){
					if(res.code == "0000"){
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 6
					   	});
						f.isSelected = null;  //前端伪改变状态
						$scope.$apply();
					} else{
						layer.msg(res.message, {
					    	offset:'t',
					      	icon: 5
					    });
					}
				}
			})
			
		}
	}
	
	//当前高亮
	$scope.index = 0;
	$scope.moduleLtemactive = function(index){
		$scope.index = index;
	}
	$scope.moduleLtemClass = function(index){
		var className = "";
		if($scope.index == index){
			className += "active";
		}
		return className;
	}
	
});
